// JavaScript Document
$(document).ready(function(){
    initDeleteBtn(".delBtn");
    initEditBtn(".editBtn");
    $(".saveBtn").click(function(){
        $("#masterNew").submit();
        return false;
    })
    $(".addBtn").click(function(){
        $("#masterNew").resetForm();
		$(".fromFiled").text("新增用户");
        $("#masterNew").attr("action", "/action/master/addMaster");
        $("#masterid").val("0");
        $("#uname").attr("disabled", false);
    })
    
    jQuery.validator.addMethod("stringCheck", function(value, element){
        return this.optional(element) || /^[\u0391-\uFFE5\w]+$/.test(value);
    }, "只能包括中文字、英文字母、数字和下划线");
    
    // 中文字两个字节       
    jQuery.validator.addMethod("byteRangeLength", function(value, element, param){
        var length = value.length;
        for (var i = 0; i < value.length; i++) {
            if (value.charCodeAt(i) > 127) {
                length++;
            }
        }
        return this.optional(element) ||
        (length >= param[0] && length <= param[1]);
    }, "请确保输入的值在3-15个字节之间(一个中文字算2个字节)");
    
    $("#masterNew").validate({
        submitHandler: function(form){
            $(form).ajaxSubmit({
                target: "#errormsg",
                dataType: "json",
                success: function(data){
                    if (!data.success) {
                        $("#errormsg").text(data.message);
                        $(".notification").removeClass("success").addClass("error").fadeTo("slow", 1).fadeIn(400).delay(4000).slideUp(400);
                    }
                    else {
                        if ($("#masterid").val() == 0) {
                            $(".listbody").append(t, {
                                uname: $("#uname").val(),
                                id: data.temp
                            });
                            $("#errormsg").text("用户添加成功！");
                            $(".notification").removeClass("error").addClass("success").fadeTo("slow", 1).fadeIn(400).delay(4000).slideUp(400);
                            initDeleteBtn("#del" + data.temp);
                            initEditBtn("#edit" + data.temp);
                            resetEvenColor();
                        }
                        else {
                            $("#errormsg").text("用户修改成功！");
                            var tid = $("#masterid").val();
                            $("#tr_" + tid).effect("highlight", {
                                color: "#FF0"
                            }, 3000);
                            $(".notification").removeClass("error").addClass("success").fadeTo("slow", 1).fadeIn(400).delay(4000).slideUp(400);
                        }
                    }
                    
                }
            });
        },
        rules: {
            uname: {
                required: true,
                byteRangeLength: [2, 20],
                stringCheck: true
            },
            upass: {
                required: true,
                minlength: 6,
                maxlength: 10
            }
        },
        messages: {
            uname: {
                required: "请填写用户名",
                byteRangeLength: "用户名必须在3-15个字符之间(一个中文字算2个字符)"
            },
            upass: {
                required: "请输入您的密码",
                minlength: "密码长度不能小于6个字符",
                maxlength: "密码长度不能大于10个字符"
            }
        }
    });
    
});

function initDeleteBtn(e){
    // $(".delBtn").easyconfirm({locale: { title: '操作确认', button: ['取消','是的，确定删除']}});
    $(e).click(function(){
        var tmp = $(this);
        $.post("/action/master/removeMaster", {
            Action: "post",
            id: tmp.attr("href")
        }, function(data, textStatus){
            if (!data.success) {
                $("#errormsg").text(data.message);
                $(".notification").removeClass("success").addClass("error").fadeTo("slow", 1).fadeIn(400).delay(4000).slideUp(400);
                ;
            }
            else {
                $("#errormsg").text("删除用户操作成功！");
                //tmp.parent().parent().remove();
                tmp.parent().parent().effect("highlight", {
                    color: "#F00"
                }, 800, function(){
                    $(this).remove();
                    resetEvenColor();
                });
                $(".notification").removeClass("error").addClass("success").fadeTo("slow", 1).fadeIn(400).delay(4000).slideUp(400);
            }
        }, "json");
        
    }).easyconfirm({
        locale: {
            title: '操作确认',
            button: ['取消', '是的，确定删除']
        }
    })
};
var resetEvenColor = function(){
    $('tbody tr').removeClass("alt-row");
    $('tbody tr:even').delay(100).addClass("alt-row");
};
function initEditBtn(e){
    $(e).click(function(){
        $("#masterNew").resetForm();
		$(".fromFiled").text("修改用户信息");
        $("#masterNew").attr("action", "/action/master/editMaster");
        $("#masterid").val($(this).attr("href"));
        $("#uname").val($(this).attr("uname"));
		$("#type").val($(this).attr("utype"));
        $("#uname").attr("disabled", true);
        $(this).attr("id", "admin" + $(this).attr("href"));
        return false;
    })
};
